﻿using System;
using Quartz;
using Spring.Scheduling.Quartz; 
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using Common.Logging;

namespace DotNetServiceLib.Core
{
    /// <summary>
    /// Task that fires an executable file. care is taken to prevent launching executable file 
    /// if it is already running.
    /// </summary>
    public class TaskExe : QuartzJobObject, ITask
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private string name = "";
        private string target = "";
        private string arguments = "";
        private bool loggingIsEnabled = true;

        protected override void ExecuteInternal(JobExecutionContext context)
        {
            if (File.Exists(target))
            {
                Process exeTask = new Process();
                try
                {
                    string fileName = Path.GetFileName(target);
                    if (!ProcessUtil.IsProcessOpen(fileName))
                    {
                        exeTask.StartInfo.UseShellExecute = false;
                        exeTask.StartInfo.FileName = target;
                        exeTask.StartInfo.Arguments = arguments;
                        exeTask.StartInfo.CreateNoWindow = true;
                        exeTask.Start();
                        if (loggingIsEnabled)
                        {
                            log.Info(String.Format("{0}, path: {1}, args: {2}, next fire time {3}",
                            name, target, arguments, context.NextFireTimeUtc.Value.ToLocalTime()));
                        }
                    }
                    else
                    {
                        log.Info(String.Format("The following process {0} is already running. To prevent abusive memory usage, no new process could be launched.", fileName));
                    }
                }
                catch (Exception exc)
                {
                    log.Info(String.Format("An error occured while executing file {0}", exc.ToString()));
                }
                finally {
                    if (exeTask != null)
                        exeTask.Close();
                }
            }
            else
            {
                log.Info(String.Format("Executable file could not be found at {0}", target));
            }
        }

        #region Miembros de ITask
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public string Target
        {
            get
            {
                return target;
            }
            set
            {
                target = value;
            }
        }
        public string Arguments
        {
            get
            {
                return arguments;
            }
            set
            {
                arguments = value;
            }
        }
        public bool LoggingIsEnabled
        {
            get
            {
                return loggingIsEnabled;
            }
            set
            {
                loggingIsEnabled = value;
            }
        }
        #endregion

        #region Miembros de ITask


        public string ClassName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string MethodName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region Miembros de ITask

        string ITask.Name
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        string ITask.Target
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        string ITask.Arguments
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        string ITask.ClassName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        string ITask.MethodName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        bool ITask.LoggingIsEnabled
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
